package net.lab1024.sa.admin.module.business.record.service;

import java.util.List;
import java.util.stream.Collectors;

import net.lab1024.sa.admin.module.business.order.domain.vo.OrderVO;
import net.lab1024.sa.admin.module.business.record.dao.RecordDao;
import net.lab1024.sa.admin.module.business.record.domain.entity.RecordEntity;
import net.lab1024.sa.admin.module.business.record.domain.form.RecordAddForm;
import net.lab1024.sa.admin.module.business.record.domain.form.RecordQueryForm;
import net.lab1024.sa.admin.module.business.record.domain.form.RecordUpdateForm;
import net.lab1024.sa.admin.module.business.record.domain.vo.RecordVO;
import net.lab1024.sa.admin.module.system.type.Dao.TypeDao;
import net.lab1024.sa.admin.module.system.type.domain.TypeEntity;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.domain.PageResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 提现 Service
 *
 * @Author syf
 * @Date 2025-05-29 11:07:16
 * @Copyright 畅想科技
 */

@Service
public class RecordService {

    @Resource
    private RecordDao recordDao;

    @Resource
    private TypeDao typeDao;
    /**
     * 分页查询
     *
     * @param queryForm
     * @return
     */
    public PageResult<RecordVO> queryPage(RecordQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<RecordVO> list = recordDao.queryPage(page, queryForm);
        List<TypeEntity> typeList = typeDao.selectByKey("payment_type");
        List<String> paymentTypeList = typeList.stream().map(TypeEntity::getTypeName).collect(Collectors.toList());
        for (RecordVO recordVO : list) {
            recordVO.setPaymentTypeName(paymentTypeList.get(recordVO.getPaymentType()));
        }
        PageResult<RecordVO> pageResult = SmartPageUtil.convert2PageResult(page, list);
        return pageResult;
    }

    /**
     * 添加
     */
    public ResponseDTO<String> add(RecordAddForm addForm) {
        RecordEntity recordEntity = SmartBeanUtil.copy(addForm, RecordEntity.class);
        recordDao.insert(recordEntity);
        return ResponseDTO.ok();
    }

    /**
     * 更新
     *
     * @param updateForm
     * @return
     */
    public ResponseDTO<String> update(RecordUpdateForm updateForm) {
        RecordEntity recordEntity = SmartBeanUtil.copy(updateForm, RecordEntity.class);
        recordDao.updateById(recordEntity);
        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     *
     * @param idList
     * @return
     */
    public ResponseDTO<String> batchDelete(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)){
            return ResponseDTO.ok();
        }

        recordDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     */
    public ResponseDTO<String> delete(Long recordId) {
        if (null == recordId){
            return ResponseDTO.ok();
        }

        recordDao.deleteById(recordId);
        return ResponseDTO.ok();
    }
}
